import { defineStore } from 'pinia'
import type { Resource, ResourceCategory } from '@/types/resource'

interface ResourceState {
  resources: Resource[]
  categories: ResourceCategory[]
  currentResource: Resource | null
  total: number
  loading: boolean
}

export const useResourceStore = defineStore('resource', {
  state: (): ResourceState => ({
    resources: [],
    categories: [],
    currentResource: null,
    total: 0,
    loading: false
  }),

  getters: {
    getResources: (state) => state.resources,
    getCategories: (state) => state.categories,
    getCurrentResource: (state) => state.currentResource,
    getTotal: (state) => state.total,
    isLoading: (state) => state.loading,
    getCategoryTree: (state) => {
      // 构建分类树结构
      const buildTree = (categories: ResourceCategory[], parentId: number | null = null): ResourceCategory[] => {
        return categories
          .filter(cat => cat.parent_id === parentId)
          .map(cat => ({
            ...cat,
            children: buildTree(categories, cat.id)
          }))
      }
      return buildTree(state.categories)
    }
  },

  actions: {
    // 获取资源列表
    async fetchResources(params?: any) {
      this.setLoading(true)
      try {
        // TODO: 调用获取资源列表API
        // const response = await getResourcesApi(params)
        // this.setResources(response.data.data)
        // this.setTotal(response.data.total)
        // return response
      } catch (error) {
        throw error
      } finally {
        this.setLoading(false)
      }
    },

    // 获取资源详情
    async fetchResource(id: number) {
      try {
        // TODO: 调用获取资源详情API
        // const response = await getResourceApi(id)
        // this.setCurrentResource(response.data)
        // return response
      } catch (error) {
        throw error
      }
    },

    // 上传资源
    async uploadResource(file: File, categoryId?: number) {
      try {
        // TODO: 调用上传资源API
        // const response = await uploadResourceApi(file, categoryId)
        // await this.fetchResources()
        // return response
      } catch (error) {
        throw error
      }
    },

    // 删除资源
    async deleteResource(id: number) {
      try {
        // TODO: 调用删除资源API
        // const response = await deleteResourceApi(id)
        // await this.fetchResources()
        // return response
      } catch (error) {
        throw error
      }
    },

    // 获取资源分类
    async fetchCategories() {
      try {
        // TODO: 调用获取资源分类API
        // const response = await getCategoriesApi()
        // this.setCategories(response.data)
        // return response
      } catch (error) {
        throw error
      }
    },

    // 创建资源分类
    async createCategory(categoryData: Partial<ResourceCategory>) {
      try {
        // TODO: 调用创建资源分类API
        // const response = await createCategoryApi(categoryData)
        // await this.fetchCategories()
        // return response
      } catch (error) {
        throw error
      }
    },

    // 更新资源分类
    async updateCategory(id: number, categoryData: Partial<ResourceCategory>) {
      try {
        // TODO: 调用更新资源分类API
        // const response = await updateCategoryApi(id, categoryData)
        // await this.fetchCategories()
        // return response
      } catch (error) {
        throw error
      }
    },

    // 删除资源分类
    async deleteCategory(id: number) {
      try {
        // TODO: 调用删除资源分类API
        // const response = await deleteCategoryApi(id)
        // await this.fetchCategories()
        // return response
      } catch (error) {
        throw error
      }
    },

    // 设置资源列表
    setResources(resources: Resource[]) {
      this.resources = resources
    },

    // 设置资源分类
    setCategories(categories: ResourceCategory[]) {
      this.categories = categories
    },

    // 设置当前资源
    setCurrentResource(resource: Resource) {
      this.currentResource = resource
    },

    // 设置总数
    setTotal(total: number) {
      this.total = total
    },

    // 设置加载状态
    setLoading(loading: boolean) {
      this.loading = loading
    },

    // 重置状态
    resetState() {
      this.resources = []
      this.categories = []
      this.currentResource = null
      this.total = 0
      this.loading = false
    }
  }
})